<?php

/**
 * 单页
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: PageController.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace Admin\Controller;
class PageController extends CommonController{
    private $model;
    private $service;
    public function _initialize() {
        parent::_initialize();
        $this->model = D("Page");
        $this->service = D("Page", "Service");
    }
    /**
     * 单页管理
     */
    public function index() {
        $result = $this->getPagination("Page");
        $this->assign('list', $result['data']);
        $this->assign('count', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display();
    }
    
    /**
     * 添加单页
     */
    public function add() {
        $this->display();
    }
    
    /**
     * 创建单页
     */
    public function create() {
        $page = I("post.page");
        if(!isArrayAndNotEmpty($page)){
            return $this->errorReturn("无效操作！");
        }
        $result = $this->service->add($page);
        if(!$result['status']){
            return $this->errorReturn($result['data']["error"]);
        }
        
        return $this->successReturn("添加单页成功!", U("index"));
    }
    
    /**
     * 修改单页
     */
    public function edit() {
        $id = I("get.id", 0, "intval");
        if($id <= 0){
            return $this->error("无效操作！");
        }
        $page = $this->service->getById($id);
        if(!isArrayAndNotEmpty($page)){
            return $this->error("您要修改的单页不存在！");
        }
        $this->assign("page", $page);
        $this->display();
    }
    /**
     * 更新
     */
    public function update() {
        $page = I("post.page");
        if(!isArrayAndNotEmpty($page)){
            return $this->errorReturn("无效操作！");
        }
        $result = $this->service->update($page);
        if(!$result['status']){
            return $this->errorReturn($result['data']["error"]);
        }
        
        return $this->successReturn("修改单页成功!", U("index"));
    }
    
    /**
     * 删除
     */
    public function delete() {
        $id = I("post.id", 0, "intval");
        if($id <= 0){
            return $this->errorReturn("无效操作！");
        }
        $page = $this->service->getById($id);
        if(!isArrayAndNotEmpty($page)){
            return $this->errorReturn("您要删除的单页不存在！");
        }
        
        if($this->service->delete($id)){
            return $this->successReturn("删除单页成功!", U("index"));
        }else{
            return $this->errorReturn("删除单页失败！");
        }
        
    }
}
